//
//  CHWelcomeView.swift
//  status
//
//  Created by 彳亍 on 2017/3/23.
//  Copyright © 2017年 彳亍. All rights reserved.
//

import UIKit
import SnapKit

class CHWelcomeView: UIView {
    
    /// 背景视图
    fileprivate lazy var imageView = UIImageView(image: UIImage(named: "ad_background"))
    /// 昵称
    fileprivate lazy var nameLabel:UILabel = UILabel(text: nil, textColor: UIColor.darkGray)
    /// 头像
    fileprivate lazy var avatarImageView  = UIImageView()
    
    /// 头像底部约束
    fileprivate var avatarBottomConstraint : ConstraintMakerEditable?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 设置UI
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func didMoveToWindow() {
        super.didMoveToWindow()
        // 设置动画
        setupAvatarAnimation()
    }
    
    
    
}

// MARK: - UI
extension CHWelcomeView {
    
    /// 设置UI
    fileprivate func setupUI() {
        // 添加视图
        addSubview(imageView)
        addSubview(avatarImageView)
        addSubview(nameLabel)
        // 布局
        imageView.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.edges.equalToSuperview()
        }
        
        avatarImageView.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.centerX.equalToSuperview()
            avatarBottomConstraint = ConstraintMaker.bottom.equalToSuperview().offset(-100)
            ConstraintMaker.width.height.equalTo(85)
        }
        
        nameLabel.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.centerX.equalToSuperview()
            ConstraintMaker.top.equalTo(avatarImageView.snp.bottom).offset(8)
        }
        
        
        // 加载网络图片
        avatarImageView.ch_setImage(URLString: CHHTTPSessionManager.shared.account.avatar_large,
                                    placeholder: UIImage(named:"avatar_default_big"),
                                    isClip: true,
                                    backgroundColor: UIColor.ch_colorWithHex(hex: 0xF8F8F8),
                                    borderColor: UIColor.darkGray,
                                    opaque: true)
        
        // 设置昵称饿
        nameLabel.text = CHHTTPSessionManager.shared.account.screen_name
        nameLabel.alpha = 0
    }
    
    /// 设置动画
    fileprivate func setupAvatarAnimation() {
        
        // 强制更新约束
        layoutIfNeeded()
        avatarBottomConstraint?.constraint.update(offset: -350)
        
        UIView.animate(withDuration: 1.5,
                       delay: 0,
                       usingSpringWithDamping: 0.7,
                       initialSpringVelocity: 0,
                       options: [],
                       animations: {
                        
                        self.layoutIfNeeded()
        }, completion: { (_) in
            
            UIView.animate(withDuration: 1.5, animations: {
                
                self.nameLabel.alpha = 1
            }, completion: { (_) in
                
                self.removeFromSuperview()
            })
        })
    }
}
